<?php

/*
 *    Document   : users
 *   Created on : Jun 11, 2011, 3:39:26 PM
 *    Author     : ClarkT AKA FalleN
 *    Company    : Amodity
 *    Web        : http://www.amodity.com
 *    Copyright  : 2011
 */

class users extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->load->model('admin/core');
        $this->core->_prev_url();
        $this->load->helper('config_file');
    }

    public function index() {
        //is user logged in
        $this->core->_is_logged_in();
        //permissions
        $this->auth->restrict('Admin_Users_View');

        $this->load->model('admin/users_model', 'users');
        $userman = $this->users->getusers();

        $data = array(
            'table' => $this->table->generate(),
        );
        $this->template->set('title', lang('admin_user_management'));
        $this->template->set('page_title', lang('admin_user_management'));
        $this->template->load('templates/admin_template', 'user_management', $data, 'admin');
    }

    function edit_user($userid) {
        //is user logged in
        $this->core->_is_logged_in();
        //permissions
        $this->auth->restrict('Admin_Users_Manage');

        if (!isset($_POST['submit'])) {
            //$userid = $this->input->post('id');

            //get user info from db

            $userdata = $this->auth->get_user($userid);

            //prepare data for output
            $data = array(
                'row' => $userdata,
            );
            $this->template->set('title', lang('admin_edit_user'));
            $this->template->load('templates/admin_template', 'edit_user', $data, 'admin');
        } else {
            $this->load->model('admin/users_model');
            $status = $this->users_model->do_edit();
            
            $this->template->set('title', lang('admin_edit_user'));
            $this->template->load('templates/admin_template', 'edit_user', 'admin');
        }
    }

    //--------------------------------------------------------------------

    function delete_user($userid) {
        //is user logged in
        $this->core->_is_logged_in();
        //permissions
        $this->auth->restrict('Admin_Users_Manage');

        $this->load->model('admin/users_model', 'users');
        $this->users->user_delete($userid);

        redirect('admin/users');
    }

    //--------------------------------------------------------------------

    function suspend_user($userid) {
        //is user logged in
        $this->core->_is_logged_in();
        //permissions
        $this->auth->restrict('Admin_Users_Manage');

        $this->db->where('id', $userid);
        $this->db->select('active');
        $query = $this->db->get('users');
        foreach ($query->result() as $row) {
            
        }
        if ($row->active == 0) {
            $this->message->set('success', lang('admin_user_suspend_fail'));
            $this->db->where('id', $userid);
            $this->db->update('users', array('active' => '1'));
        } else {
            $this->message->set('success', lang('admin_user_suspend_success'));
            $this->db->where('id', $userid);
            $this->db->update('users', array('active' => '0'));
        }
        redirect('admin/users');
    }

    //--------------------------------------------------------------------

    function usergroups() {
        //is user logged in
        $this->core->_is_logged_in();
        //check perms
        $this->auth->restrict('Admin_Roles_View');

        $this->load->model('admin/users_model', 'users');
        $groups = $this->users->usergroups();

        $data = array(
            'table' => $this->table->generate()
        );
        $this->template->set('title', lang('admin_usergroups'));
        $this->template->set('page_title', lang('admin_usergroups'));
        $this->template->load('templates/admin_template', 'usergroups', $data, 'admin');
    }

}

?>